package com.vmc.model;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vmc.exception.VMCException;

public class MovieTransferDetail extends AbstractTransferDetail {

	private String jsonFilePath;
	private String[] subtitleFilesPath;
	private String indexFilePath;
	private static ObjectMapper mapper = new ObjectMapper();
	
	public MovieTransferDetail(String title, String extension, String location,
			String sourceFilename) {
		super(title, extension, location, sourceFilename, DetailType.Movie);
	}
	
	public MovieTransferDetail() {
		super(null, null, null, null, DetailType.Movie);
	}

	public String getJsonFilePath() {
		return jsonFilePath;
	}
	public void setJsonFilePath(String jsonFilePath) {
		this.jsonFilePath = jsonFilePath;
	}
	
	public String[] getSubtitleFilesPath() {
		return subtitleFilesPath;
	}

	public void setSubtitleFilesPath(String[] subtitleFilesPath) {
		this.subtitleFilesPath = subtitleFilesPath;
	}

	public String getIndexFilePath() {
		return indexFilePath;
	}

	public void setIndexFilePath(String indexFilePath) {
		this.indexFilePath = indexFilePath;
	}

	@Override
	public String getDestinationFilePath() {
		return String.format(FORMAT, this.getLocation(), this.getTitle(), this.getExtension());
	}

	public static class Builder {
		public static MovieTransferDetail build(File jsonFile) throws VMCException {
			MovieTransferDetail detail = null;
			if (jsonFile != null && jsonFile.exists() && jsonFile.isFile() && mapper != null) {
				try {
					detail = mapper.readValue(jsonFile, MovieTransferDetail.class);
				} catch (JsonParseException e) {
					throw new VMCException(VMCException.PARSE_ERROR, jsonFile.getAbsolutePath());
				} catch (JsonMappingException e) {
					throw new VMCException(VMCException.MAPPING_ERROR, jsonFile.getAbsolutePath());
				} catch (IOException e) {
					throw new VMCException(VMCException.JSON_IO_ERROR, jsonFile.getAbsolutePath());
				}
				detail.setJsonFilePath(jsonFile.getAbsolutePath());				
			}
			return detail;
		}
	}
}
